package net.sourceforge.m2settings.model;

import javax.swing.table.AbstractTableModel;

import org.apache.log4j.Logger;

public class ServerTableModel extends AbstractTableModel implements ExtendableTableModel {
	private static final long serialVersionUID = 7077417824183796168L;
	private static final int COLUMN_ID = 0;
	private static final int COLUMN_USERNAME = 1;
	private static final int COLUMN_PASSWORD = 2;
	private static final int COLUMN_PRIVATEKEY = 3;
	private static final int COLUMN_PASSPHRASE = 4;
	private static final int COLUMN_FILEPERMISSIONS = 5;
	private static final int COLUMN_DIRECTORYPERMISSIONS = 6;
	private static final int COLUMN_CONFIGURATION = 7;
	
	private Logger log = Logger.getLogger(ServerTableModel.class);
	private Servers servers;
	private String[] columnTitles;
	
	public ServerTableModel(Settings userSettings) {
		log.debug("Create ServersTableModel with settings: "+userSettings.toString());
		servers = userSettings.getServers();
		if (servers==null) { //no servers declared in XML
			servers=new Servers();
		}
		log.debug("there are "+servers.getServerCount()+" servers");
		columnTitles = new String[8];
		columnTitles[COLUMN_ID] = "id";
		columnTitles[COLUMN_USERNAME] = "username";
		columnTitles[COLUMN_PASSWORD] = "password";
		columnTitles[COLUMN_PRIVATEKEY] = "private key";
		columnTitles[COLUMN_PASSPHRASE] = "passphrase";
		columnTitles[COLUMN_FILEPERMISSIONS] = "file permissions";
		columnTitles[COLUMN_DIRECTORYPERMISSIONS] = "directory permissions";
		columnTitles[COLUMN_CONFIGURATION] = "configuration";
	}
	public void addRow() {
		Server vServer=new Server();;
		servers.addServer(vServer);
		// notifies listeners that the last row is new
		fireTableRowsInserted(getRowCount(), getRowCount());
	}

	public void removeRows(int[] rowIndexes) {
		for (int i = rowIndexes.length - 1; i >= 0; i--) {
			int row=rowIndexes[i];
			Server vServer = servers.getServer(row);
			servers.removeServer(vServer);
			fireTableRowsDeleted(row,row);
		}
	}

	public int getColumnCount() {
		return columnTitles.length;
	}

	public int getRowCount() {
		return servers.getServerCount();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		Server row =getRow(rowIndex);
		switch (columnIndex) {
		case COLUMN_ID:
			return row.getId();
		case COLUMN_USERNAME:
			return row.getUsername();
		case COLUMN_PASSWORD:
			return row.getPassword();
		case COLUMN_PASSPHRASE:
			return row.getPassphrase();
		case COLUMN_CONFIGURATION:
			return "configuration";//TODO row.getConfiguration();
		case COLUMN_PRIVATEKEY:
			return row.getPrivateKey();
		case COLUMN_FILEPERMISSIONS:
			return row.getFilePermissions();
		case COLUMN_DIRECTORYPERMISSIONS:
			return row.getDirectoryPermissions();
		}
		return null;
	}
	
	private Server getRow(int rowIndex) {
		return servers.getServer(rowIndex);
	}

	@Override
	public String getColumnName(int column) {
		return columnTitles[column];
	}

	public String getElementName() {
		return "server";
	}
	
	@Override
	public void setValueAt(Object object, int row, int col) {
		Server server =getRow(row);
		assert (String.class == object.getClass());
		String value=(String)object;
		switch (col) {
		case COLUMN_ID:
			server.setId(value);
		case COLUMN_USERNAME:
			server.setUsername(value);
		case COLUMN_PASSWORD:
			server.setPassword(value);
		case COLUMN_PASSPHRASE:
			server.setPassphrase(value);
		case COLUMN_CONFIGURATION:
			//TODO setConfiguration();
		case COLUMN_PRIVATEKEY:
			server.setPrivateKey(value);
		case COLUMN_FILEPERMISSIONS:
			server.setFilePermissions(value);
		case COLUMN_DIRECTORYPERMISSIONS:
			server.setDirectoryPermissions(value);
		}		
	}
}
